﻿using RRQMMVVM;
using System.Windows;

namespace ViewModel.WindowDialog
{
    public class WindowDialogViewModel : ViewModelBase
    {
        public WindowDialogViewModel()
        {
            this.CloseSearchFriendCommand = new ExecuteCommand(CloseSearchFriend);
            this.ClosePersonalSettingCommand = new ExecuteCommand(ClosePersonalSetting);
            this.CloseSearchFriendInfoCommand = new ExecuteCommand(CloseSearchFriendInfo);
            this.CloseDisconnectionAndResumeCommand = new ExecuteCommand(CloseDisconnectionAndResume);
            this.RequestAddFriendCommand = new ExecuteCommand(RequestAddFriend);
            this.ReconnectionCommand = new ExecuteCommand(Reconnection);
            this.UpdataNicknameCommand = new ExecuteCommand(UpdataNickname);
            this.LaunchSearchFriendCommand = new ExecuteCommand(LaunchSearchFriend);
        }



        #region Command
        public ExecuteCommand CloseSearchFriendCommand { get; set; }
        public ExecuteCommand ClosePersonalSettingCommand { get; set; }
        public ExecuteCommand CloseSearchFriendInfoCommand { get; set; }
        public ExecuteCommand CloseDisconnectionAndResumeCommand { get; set; }

        public ExecuteCommand ReconnectionCommand { get; set; }
        public ExecuteCommand UpdataNicknameCommand { get; set; }

        public ExecuteCommand LaunchSearchFriendCommand { get; set; }
        public ExecuteCommand RequestAddFriendCommand { get; set; }

        #endregion Command

        #region 属性

        private Visibility dialogVisible = Visibility.Hidden;

        /// <summary>
        /// 对话框主面板显示
        /// </summary>
        public Visibility DialogVisible
        {
            get { return dialogVisible; }
            set
            {
                dialogVisible = value;
                OnPropertyChanged();
            }
        }

        private Visibility searchFriendVisible = Visibility.Hidden;

        /// <summary>
        /// 搜索好友面板显示
        /// </summary>
        public Visibility SearchFriendVisible
        {
            get { return searchFriendVisible; }
            set { searchFriendVisible = value; OnPropertyChanged(); }
        }

        private string searchFriendText = "请输入号码";

        /// <summary>
        /// 添加好友号码
        /// </summary>
        public string SearchFriendText
        {
            get { return searchFriendText; }
            set { searchFriendText = value; OnPropertyChanged(); }
        }

        private Visibility personalSettingVisible = Visibility.Hidden;

        /// <summary>
        /// 个人设置面板显示
        /// </summary>
        public Visibility PersonalSettingVisible
        {
            get { return personalSettingVisible; }
            set { personalSettingVisible = value; OnPropertyChanged(); }
        }

        private string nickname;

        /// <summary>
        /// 昵称
        /// </summary>
        public string Nickname
        {
            get { return nickname; }
            set { nickname = value; OnPropertyChanged(); }
        }

        private string tempNickname;

        /// <summary>
        /// 修改的昵称
        /// </summary>
        public string TempNickname
        {
            get { return tempNickname; }
            set { tempNickname = value; OnPropertyChanged(); }
        }

        private string searchFriendMessage;

        /// <summary>
        /// 搜索好友的返回信息
        /// </summary>
        public string SearchFriendMessage
        {
            get { return searchFriendMessage; }
            set { searchFriendMessage = value; OnPropertyChanged(); }
        }

        private Visibility searchFriendInfoVisible = Visibility.Hidden;

        /// <summary>
        /// 搜索到的信息面板显示
        /// </summary>
        public Visibility SearchFriendInfoVisible
        {
            get { return searchFriendInfoVisible; }
            set { searchFriendInfoVisible = value; OnPropertyChanged(); }
        }

        private string searchedNickname;

        /// <summary>
        /// 查询到的昵称
        /// </summary>
        public string SearchedNickname
        {
            get { return searchedNickname; }
            set { searchedNickname = value; OnPropertyChanged(); }
        }

        private int searchedAccount;

        public int SearchedAccount
        {
            get { return searchedAccount; }
            set { searchedAccount = value; OnPropertyChanged(); }
        }

        private Visibility disconnectionAndResumeVisible = Visibility.Hidden;

        public Visibility DisconnectionAndResumeVisible
        {
            get { return disconnectionAndResumeVisible; }
            set { disconnectionAndResumeVisible = value; OnPropertyChanged(); }
        }

        #endregion 属性

        #region 公共方法

        /// <summary>
        /// 网络断开时
        /// </summary>
        public void DisConnected()
        {
            this.DisconnectionAndResumeVisible = Visibility.Visible;
            this.DialogVisible = Visibility.Visible;
        }

        /// <summary>
        /// 网络恢复
        /// </summary>
        public void ResumeConnected()
        {
            CloseDisconnectionAndResume();
        }

        #endregion 公共方法

        #region 绑定方法

        /// <summary>
        /// 关闭查找好友界面
        /// </summary>
        private void CloseSearchFriend()
        {
            this.SearchFriendVisible = Visibility.Hidden;
            this.DialogVisible = Visibility.Hidden;
        }

        /// <summary>
        /// 关闭个人设置面板
        /// </summary>
        private void ClosePersonalSetting()
        {
            this.PersonalSettingVisible = Visibility.Hidden;
            this.DialogVisible = Visibility.Hidden;
        }

        /// <summary>
        /// 关闭查找好友信息面板
        /// </summary>
        private void CloseSearchFriendInfo()
        {
            this.SearchFriendInfoVisible = Visibility.Hidden;
            this.DialogVisible = Visibility.Hidden;
        }

        private void CloseDisconnectionAndResume()
        {
            this.DisconnectionAndResumeVisible = Visibility.Hidden;
            this.DialogVisible = Visibility.Hidden;
        }

        private void UpdataNickname()
        {
            //Userinfo userinfo = new Userinfo();
            //userinfo.Nickname = this.TempNickname;
            //Confirm confirm = TransferClient.UpdataUserinfo(userinfo);
            //if (confirm!=null&& confirm.Status)
            //{
            //    this.Nickname = this.TempNickname;
            //}
        }

        private void LaunchSearchFriend()
        {
            //int account;
            //if (int.TryParse(this.searchFriendText, out account))
            //{
            //    Userinfo userinfo = TransferClient.GetFriendinfo(account);
            //    if (userinfo != null)
            //    {
            //        if (userinfo.Status)
            //        {
            //            this.SearchFriendInfoVisible = Visibility.Visible;
            //            this.SearchFriendVisible = Visibility.Hidden;
            //            this.SearchedNickname = userinfo.Nickname;
            //            this.SearchedAccount = account;
            //        }
            //        else
            //        {
            //            this.SearchFriendMessage = userinfo.Message;
            //        }
            //    }
            //    else
            //    {
            //        this.SearchFriendMessage = "未知错误";
            //    }
            //}
        }

        private void RequestAddFriend()
        {
            //TransferMessage transferMessage = new TransferMessage();
            //transferMessage.TargetAccount = SearchedAccount;
            //Confirm confirm = TransferClient.RequestAddFriend(transferMessage);
            //if (confirm!=null&& confirm.Status)
            //{
            //    CloseSearchFriendInfo();
            //}
        }

        private void Reconnection()
        {
            //if (TransferClient.ConnectService())
            //{
            //   Confirm confirm= TransferClient.ResumeConnect();
            //    if (confirm!=null&& confirm.Status)
            //    {
            //        CloseDisconnectionAndResume();
            //    }
            //}
        }

        #endregion 绑定方法
    }
}